//
//  LDSearchViewController.m
//  renmingongshe
//
//  Created by macbook on 2017/10/25.
//  Copyright © 2017年 xiaodian. All rights reserved.
//

#import "LDSearchViewController.h"
#import "selectLabelView.h"
#import "LineDownHomeTCell.h"
#import "LDStoreViewController.h"

@interface LDSearchViewController ()<UISearchBarDelegate,UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) UISearchBar *searchBar;
@property (strong, nonatomic) selectLabelView *labelView;
@property (strong, nonatomic) UITableView * tableView;
@property (strong, nonatomic) UIView *footerView;
@property (strong, nonatomic) NSMutableArray *ListArr;

@end

@implementation LDSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.titleLabel.text = @"搜索";
    _ListArr = [NSMutableArray array];
    [self createSearchBar];
    [self createFooterView];
    [self createTableView];
}

- (void)createSearchBar
{
    _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(10, 80, WIDTH-20, 30)];
    _searchBar.backgroundColor = [UIColor clearColor];
    _searchBar.showsCancelButton = NO;
    _searchBar.delegate = self;
    _searchBar.placeholder = @"";
    
    for (UIView *subView in _searchBar.subviews) {
        if ([subView isKindOfClass:[UIView  class]]) {
            [[subView.subviews objectAtIndex:0] removeFromSuperview];
            if ([[subView.subviews objectAtIndex:0] isKindOfClass:[UITextField class]]) {
                UITextField *textField = [subView.subviews objectAtIndex:0];
                textField.backgroundColor = [UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1];
                
                //设置默认文字颜色
                UIColor *color = [UIColor grayColor];
                [textField setAttributedPlaceholder:[[NSAttributedString alloc] initWithString:@"搜索商品/店铺" attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:14.0f], NSForegroundColorAttributeName:color}]];
                //修改默认的放大镜图片
                UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 13, 13)];
                imageView.backgroundColor = [UIColor clearColor];
                imageView.image = [UIImage imageNamed:@"LineDownSearch"];
                textField.leftView = imageView;
            }
        }
    }
    [self.view addSubview:_searchBar];
}

- (void)downloadData
{
    [self.view loadingOnAnyView];
    [THWManager PostHttp:API(LDSearch) Parameters:@{@"keyword":_searchBar.text, @"jwd":[Utils getJWD]} Success:^(id responseObject) {
        [self.view removeAnyView];
        if ([[[[Utils getData:responseObject] objectForKey:@"lineList"] objectForKey:@"Rows"] isKindOfClass:[NSArray class]]) {
            _ListArr = [[[Utils getData:responseObject] objectForKey:@"lineList"] objectForKey:@"Rows"];
        }
        
        [_tableView reloadData];
    } andFalse:^(NSError *error) {
        [self.view removeAnyView];
    }];
}

#pragma mark-seacherDelegate
//回车键或者键盘的return触发
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [self downloadData];
}

- (void)createFooterView
{
    _footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 1)];
    _footerView.backgroundColor = RGB(232, 234, 234);
}


- (void)createTableView
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 120, WIDTH, HEIGHT-120)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerNib:[UINib nibWithNibName:@"LineDownHomeTCell" bundle:nil] forCellReuseIdentifier:@"LineDownHomeTCell"];
    [_tableView setTableFooterView:_footerView];
    _tableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_tableView];
}

#pragma mark - UITableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _ListArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LineDownHomeTCell*cell = [tableView dequeueReusableCellWithIdentifier:@"LineDownHomeTCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[LineDownHomeTCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"LineDownHomeTCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell cellConfigureModel:_ListArr[indexPath.row]];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 111;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    LDStoreViewController *store = [[LDStoreViewController alloc] init];
    store.lineId = [_ListArr[indexPath.row] objectForKey:@"lineId"];
    [self.navigationController pushViewController:store animated:YES];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [_searchBar resignFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
